//
//  HD_LiDa_LrcTable.m
//  HDMusicDemo
//
//  Created by Lida on 2017/8/14.
//  Copyright © 2017年 Lida. All rights reserved.
//

#import "HD_LiDa_LrcTable.h"
#import "HD_LiDa_Playermanager.h"
#import "HD_LiDa_PlayerTool.h"
#import <MediaPlayer/MediaPlayer.h>
@implementation HD_LiDa_LrcTable

- (void)dealloc{
    [self removeFromSuperview];
    self.lrcTimeArr = nil;
    self.lrcDic = nil;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.lrcDic = [HD_LiDa_Playermanager sharedHD_LiDa_Playermanager].lrcDic;
        self.lrcTimeArr = [HD_LiDa_Playermanager sharedHD_LiDa_Playermanager].lrcTimeArr;
        self.currentIndex = 0;
        self.backgroundColor = [UIColor clearColor];
        self.separatorColor = [UIColor clearColor];
        self.delegate = self;
        self.dataSource = self;
        
    }
    return self;
}

#pragma mark tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.lrcTimeArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    if (indexPath.row == _currentIndex) {
        cell.textLabel.textColor = [UIColor redColor];
    }else{
        cell.textLabel.textColor = [UIColor whiteColor];
        
    }
    cell.textLabel.text = [self.lrcDic objectForKey:self.lrcTimeArr[indexPath.row]];
    
    return cell;
}

- (void)refreshWithIndex:(NSInteger)index{
    for (int i = 0; i < self.lrcTimeArr.count; i++) {
        NSString *timeStr = self.lrcTimeArr[i];
        if ([timeStr integerValue] == index) {
            _currentIndex = i;
            [self resetLockInfo];
            [self reloadData];
            [self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
            return;
        }
    }
    
}

- (void)resetLockInfo
{
    HD_LiDa_PlayerTool *playTool = [HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool];
    MusicInfo *currentModel = [HD_LiDa_Playermanager sharedHD_LiDa_Playermanager].currentMusic;
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    //设置专辑名称
    info[MPMediaItemPropertyAlbumTitle] = @"网络热曲";
    //设置歌曲名
    info[MPMediaItemPropertyTitle] = currentModel.musicName;
    //设置歌手
    info[MPMediaItemPropertyArtist] = currentModel.songer;
    //将专辑图片与歌词截屏作为锁屏歌词（暂时没查到别的方法）
    //UIImage *image = [UIImage imageNamed:music.icon];
    UIImage *image = [self createShareImage:[self.lrcDic objectForKey:self.lrcTimeArr[_currentIndex]] name:currentModel.icon number:nil grade:nil];
    MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
    info[MPMediaItemPropertyArtwork] = artwork;
    //设置歌曲时间
    CGFloat time = playTool.totalTime- playTool.currentTime;
    info[MPMediaItemPropertyPlaybackDuration] = @(playTool.totalTime);
    info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(playTool.currentTime);
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;
    
}

- ( UIImage *)createShareImage:( NSString *)str name:( NSString *)name number:( NSString *)number grade:( NSString *)grade
{
    
    UIImage *image = [UIImage imageNamed:name];
    CGSize size= CGSizeMake (image.size.width, image.size.height); // 画布大小
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [image drawAtPoint:CGPointMake(0, 0)];
    
    // 获得一个位图图形上下文
    CGContextRef context= UIGraphicsGetCurrentContext ();
    CGContextDrawPath(context, kCGPathStroke );
    CGSize needSize = CGSizeMake(1000,20);
    NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor redColor]};
    CGSize labelsize = [str boundingRectWithSize:needSize options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    CGFloat width = MIN(labelsize.width, image.size.width);
    [str drawInRect:CGRectMake((image.size.width-width)/2, image.size.height - 20, width, 20) withAttributes:attribute];
    
    // 返回绘制的新图形
    UIImage *newImage= UIGraphicsGetImageFromCurrentImageContext ();
    UIGraphicsEndImageContext ();
    
    return newImage;
}



/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
